package chenji.watcher.core;

import java.util.HashMap;

import org.bukkit.Location;
import org.bukkit.entity.Player;

import chenji.watcher.Main;

public class WatchGroupEditorManager {
	private HashMap<Player, WatchGroupData> editorcache = new HashMap<>();

	public void editWatchGroupData(Player p, WatchGroupData wgd) {
		if(this.editorcache.containsKey(p)){
			this.save(p);
		}
		this.editorcache.put(p, wgd);
	}

	public boolean addNewTeleporter(Player p, String name, String Title, int time, Location loc) {
		if (!(this.editorcache.containsKey(p))) {
			return false;
		}
		WatchGroupData data = editorcache.get(p);
		if (!data.containTeleporter(name)) {
			p.sendMessage("§c[Watcher] §a该传送点名称已经存在！");
		}
		WatchGroupTeleporter wgt = new WatchGroupTeleporter(name, Title, time, loc);
		this.editorcache.get(p).registerTeleporter(wgt);
		p.sendMessage("§c[Watcher] §a创建成功");
		return true;
	}

	public void removeTeleporter(String name, Player p) {
		if (!(this.editorcache.containsKey(p))) {
			return;
		}
		WatchGroupData data = editorcache.get(p);
		if (!data.removeTeleporter(name)) {
			p.sendMessage("§c[Watcher] §a该传送点不存在");
		} else {
			p.sendMessage("§c[Watcher] §a删除成功");
		}
	}

	public void setStartTitle(Player p, String startTitle) {
		if (!(this.editorcache.containsKey(p))) {
			return;
		}
		WatchGroupData data = editorcache.get(p);
		data.setStartTitle(startTitle);
		p.sendMessage("§c[Watcher] §a设置成功");
	}

	public void setEndTitle(Player p, String endTitle) {
		if (!(this.editorcache.containsKey(p))) {
			return;
		}
		WatchGroupData data = editorcache.get(p);
		data.setEndTitle(endTitle);
		p.sendMessage("§c[Watcher] §a设置成功");
	}

	public void setEndTime(Player p, int time) {
		if (!(this.editorcache.containsKey(p))) {
			return;
		}
		WatchGroupData data = editorcache.get(p);
		data.setEndTime(time);
		p.sendMessage("§c[Watcher] §a设置成功");
	}

	public void setStartTime(Player p, int time) {
		if (!(this.editorcache.containsKey(p))) {
			return;
		}
		WatchGroupData data = editorcache.get(p);
		data.setStartTime(time);
		p.sendMessage("§c[Watcher] §a设置成功");
	}

	public void save(Player p) {
		if (!(this.editorcache.containsKey(p))) {
			return;
		}
		WatchGroupData data = editorcache.get(p);
		try {
			Main.getConfigManager().setWatchGroupData(data);
			p.sendMessage("§c[Watcher]§a保存成功");
			Main.getWatchGroupManager().setWatchGroupData(data);
		} catch (ConfigException e) {
			e.printStackTrace();
		}
	}

	public boolean contain(Player p) {
		return this.editorcache.containsKey(p);
	}

	public void print(Player p) {
		if (!(this.editorcache.containsKey(p))) {
			return;
		}
		WatchGroupData data = editorcache.get(p);
		p.sendMessage("§c----------§aWatcher 观察组信息§c----------");
		p.sendMessage("§c组名称：§c" + data.getName());
		p.sendMessage("§c开始字幕：§c" + this.replaceNull(data.getStartTitle()));
		p.sendMessage("§c开始字幕时间：§c" + data.getStartTime());
		p.sendMessage("§c结束字幕：§c" + this.replaceNull(data.getEndTitle()));
		p.sendMessage("§c结束字幕时间：§c" + data.getEndTime());
		if (data.getTPS().size() == 0) {
			p.sendMessage("§c传送点列表: §b空");
		} else {
			StringBuilder sb = new StringBuilder();
			sb.append("§c传送点列表(按照先后顺序排名): §a");
			for (WatchGroupTeleporter wgt : data.getTPS()) {
				sb.append("§a,§c");
				sb.append(wgt.getName());
			}
			p.sendMessage(sb.toString());
		}
		
		p.sendMessage("§c如果想查询传送点信息，请输入§a/watcher look [传送点名称]");
		p.sendMessage("§c---------------------------------------------");
	}

	public void print(Player p, String name) {
		if (!(this.editorcache.containsKey(p))) {
			return;
		}
		WatchGroupData data = editorcache.get(p);
		WatchGroupTeleporter wgt = data.getTeleporter(name);
		if (wgt == null) {
			p.sendMessage("§c[Wather] §a该传送点不存在");
		} else {
			p.sendMessage("§c----------§aWatcher 传送点信息§c----------");
			p.sendMessage("§c组名称：§c" + data.getName());
			p.sendMessage("§c传送点名称：§c" + wgt.getName());
			p.sendMessage("§c字幕：§c" + this.replaceNull(wgt.getTitle()));
			p.sendMessage("§c等待时间：§c" + wgt.getWaitTime());
			p.sendMessage("§c位置：§c" + wgt.getLocation().toString());
			p.sendMessage("§c如果想查询观察组信息，请输入§a/watcher look");
			p.sendMessage("§c---------------------------------------------");
		}
	}

	private String replaceNull(String re) {
		if (re == null) {
			return "§b空";
		}
		
		if (re == "") {
			return "§b空";
		}
		return re;
	}

	public void setBack(Player p, boolean b) {
		if (!(this.editorcache.containsKey(p))) {
			return;
		}
		WatchGroupData data = editorcache.get(p);
		data.setBack(b);
		p.sendMessage("§c[Watcher] §a设置成功");
	}

	public void removeWatchGroup(String string, Player p) {
		if (Main.getConfigManager().contains(string)) {
			try {
				Main.getConfigManager().remove(string);
			} catch (ConfigException e) {
				p.sendMessage("§c[Watcher] §a删除失败");
				e.printStackTrace();
			}
		} else {
			p.sendMessage("§c[Watcher] §a那个组不存在");
		}

	}

	public void createWatchGroup(Player p, String string) {
		if (Main.getConfigManager().contains(string)) {
			p.sendMessage("§c[Watcher] §a这个组已经存在了。");
		} else {
			WatchGroupData wgd = new WatchGroupData("", 10, "", 10, true, string);
			this.editWatchGroupData(p, wgd);
			p.sendMessage("§c[Watcher] §a创建成功，现在你可以开始编辑这个组了，如果需要请输入/watcher 获取编辑教程");
		}

	}

	public void quitPlayer(Player player) {
		if(this.editorcache.containsKey(player)){
			this.save(player);
		}
		
	}

}
